﻿using System;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

namespace openSourceC.DaylifeClient
{
	/// <summary>
	///		Summary description for Article.
	/// </summary>
	[Serializable]
	[XmlType("article")]
	public class Article
	{
		/// <summary></summary>
		[XmlElement("article_id")]
		public string ArticleId { get; set; }

		/// <summary></summary>
		[XmlElement("article_topic_relevance")]
		public int? ArticleTopicRelevance { get; set; }

		/// <summary></summary>
		[XmlElement("daylife_url")]
		public string DaylifeUrl { get; set; }

		/// <summary></summary>
		[XmlElement("excerpt")]
		public string Excerpt { get; set; }

		/// <summary></summary>
		[XmlElement("headline")]
		public string Headline { get; set; }

		/// <summary></summary>
		[XmlElement("image")]
		public Image Image { get; set; }

		/// <summary></summary>
		[XmlElement("related_article_count")]
		public int? RelatedArticleCount { get; set; }

		/// <summary></summary>
		[XmlElement("scores")]
		public Scores Scores { get; set; }

		/// <summary></summary>
		[XmlElement("source")]
		public Source Source { get; set; }

		/// <summary>Gets or sets the time stamp of the article expressed as a <see cref="DateTimeOffset"/>.</summary>
		[XmlIgnore]
		public DateTimeOffset? TimestampValue { get; set; }

		/// <summary>Gets or sets the time stamp of the article expressed in UTC.</summary>
		[XmlElement("timestamp")]
		public string Timestamp
		{
			get { return TimestampValue.HasValue ? TimestampValue.Value.ToString(DaylifeExtensions.TimestampFormat) : null; }

			set
			{
				if (string.IsNullOrEmpty(value))
				{
					TimestampValue = null;
				}
				else
				{
					TimestampValue = DateTimeOffset.ParseExact(value, DaylifeExtensions.TimestampFormat, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
				}
			}
		}

		/// <summary>Gets or sets the time stamp epoch (UNIX time) expressed in UTC.</summary>
		[XmlElement("timestamp_epoch")]
		public int? TimestampEpochValue { get; set; }

		/// <summary>Gets or sets the Url of the article.</summary>
		[XmlElement("url")]
		public string Url { get; set; }


		/// <summary>Gets or sets the array of all the out-of-band elements.</summary>
		[XmlAnyElement]
		public XmlElement[] ZElements { get; set; }
	}
}
